package cz.studio.baseDao;

import org.hibernate.Session;
import org.hibernate.Transaction;

public abstract class AbstractDao<T, I> {
	
	public Session hs() {
		return BasicServiceFactory.getHibernateSession();
	}
	
	@SuppressWarnings("unchecked")
	public I persis(T t) {
		Transaction tr = hs().beginTransaction();
		I id = (I) hs().save(t);
		tr.commit();
		return id;
	}
	
	public void delete(T t) {
		Transaction tr = hs().beginTransaction();
		try {
			hs().delete(t);
		} catch (Exception e) {
			e.printStackTrace();
			tr.rollback();
		}
		tr.commit();
	}
	
	public void update(T t) {
		Transaction tr = hs().beginTransaction();
		hs().update(t);
		tr.commit();
	}
}
